package joda;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * XXX
 *
 * @author mengzhang6
 * @date 2018/11/28 14:06
 */
public class JodaDateUtil {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT = "yyyyMMddHHmmss";
    public static final String FORMAT_YMD_HMS = "yyyy年MM月dd日HH时mm分ss秒";

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DAY_FORMAT2 = "yyyyMMdd";
    public static final String OUTPUT_FORMAT = "yyyy年MM月dd日";

    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String TIME_FORMAT2 = "HHmmss";

    private JodaDateUtil() {
    }

    public static Date strDate(String str, String format) {
        if (StringUtils.isEmpty(str)) {
            return nowDate();
        }
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }

        DateTime dateTime = DateTime.parse(str, DateTimeFormat.forPattern(format));
        return dateTime.toDate();
    }

    public static Date strDate(String str, String format, String x) {
        if (StringUtils.isEmpty(str)) {
            return nowDate();
        }
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }

        DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().appendPattern(format).toFormatter();
        LocalDateTime localDateTime = LocalDateTime.parse(str, dateTimeFormatter);
        return localDateTime.toDate();
    }


    public static Date strDate(String str) {
        return strDate(str, null);
    }

    public static String dateStr(Date date, String format) {
        if (null == date) {
            return now(format);
        }
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(DateTimeFormat.forPattern(format));
    }

    public static String dateStr(Date date) {
        return dateStr(date, null);
    }

    public static String now(String format) {
        if (StringUtils.isEmpty(format)) {
            format = DEFAULT_FORMAT;
        }
        return DateTime.now().toString(DateTimeFormat.forPattern(format));
    }

    public static String now() {
        return now(null);
    }

    public static Date nowDate() {
        return DateTime.now().toDate();
    }

    public static Integer getWeek(String date) {
        DateTime dateTime = DateTime.parse(date, DateTimeFormat.forPattern(DEFAULT_DATE_FORMAT));
        return dateTime.getDayOfWeek();
    }


    public static void main(String[] args) {
        System.out.println(getWeek("2017-09-11"));
    }
}
